# _*_ coding : utf-8 _*_
# @Time : 2023/8/27 17:12
# @Author : 寻梦
# @File : 05_requests_cookies登录古诗文网
# @Project : code

'''
（1）通过登录 进入主页面
    ①找到登录接口：https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx
    ②得到请求参数（data）：
        __VIEWSTATE: ypOkvu1ZSNQNQpKvC9si0uKZdUMblK8xJe+NuIZrDXtOlYtptfd40Sh8oCFqGouTC1q3Xk/+rpi72TQL7qfDPqZGDx0/kX+3Rq5UUXDjKb0J3dRrLT2zjNa9pSJh/3aT0OPqUbg24/CUADukKwCUkODlgvc=
        __VIEWSTATEGENERATOR: C93BE1AE
        from: http://so.gushiwen.cn/user/collect.aspx
        email: xmliuteng@163.com
        pwd: lt204330
        code: vrpx
        denglu: 登录
    ③难点：
        1）找 __VIEWSTATE 和 __VIEWSTATEGENERATOR （在当前登录页面中 input添加了hidden属性，直接解析出来就行）
        2）图片验证码
'''
import urllib.request

import requests
from lxml import etree

# (1) 通过get请求获取登录页源码数据，得到__VIEWSTATE和__VIEWSTATEGENERATOR
url="https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx"
headers={
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 SLBrowser/8.0.1.5162 SLBChan/30"
}
data={
        "__VIEWSTATE": '',
        "__VIEWSTATEGENERATOR": '',
        "from": "http://so.gushiwen.cn/user/collect.aspx",
        "email": 'xmliuteng@163.com',
        "pwd": 'lt204330',
        "code":'',
        "denglu": "登录"
}
response=requests.get(url=url,headers=headers)

login_index_tree=etree.HTML(response.text)

# 获取__VIEWSTATE
__VIEWSTATE=login_index_tree.xpath('//input[@id="__VIEWSTATE"]/@value')[0]
data["__VIEWSTATE"]=__VIEWSTATE
# 获取__VIEWSTATEGENERATOR
__VIEWSTATEGENERATOR=login_index_tree.xpath('//input[@id="__VIEWSTATEGENERATOR"]/@value')[0]
data["__VIEWSTATEGENERATOR"]=__VIEWSTATEGENERATOR
# 获取验证码图片地址
qrcode_image_url="https://so.gushiwen.cn"+login_index_tree.xpath('//img[@id="imgCode"]/@src')[0]
print(qrcode_image_url)

# 为了保证访问验证码图片是是同一次会话，因此使用同一次session进行访问
session = requests.session()
response_code=session.get(qrcode_image_url)
content=response_code.content
with open('code.jpg','wb') as fp:
        fp.write(content)
# 获取验证码图片中的验证码
# data["email"]=input("请输入你的登录邮箱：")
# data["pwd"]=input("请输入你的登录密码：")
data["code"]=input("请输入你的验证码：")

# 执行post请求
response= session.post(url=url,data=data,headers=headers)
with open('gushiweng_long.html','w',encoding="utf8") as fp:
        fp.write(response.text)